// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

/*.run(function($ionicPlatform) {*/
.run(['$ionicPlatform', '$ionicPopup','$rootScope','$location','$ionicLoading', function ($ionicPlatform, $ionicPopup, $rootScope, $location,$ionicLoading) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }


      $rootScope.show = function() {
          $ionicLoading.show({
              template: 'Loading...'
          });
      };
      $rootScope.hide = function(){
          $ionicLoading.hide();
      };

  });

        //主页面显示退出提示框
    $ionicPlatform.registerBackButtonAction(function (e) {

         e.preventDefault();


         function showConfirm() {
                 var confirmPopup = $ionicPopup.confirm({
                         title: '<strong>退出系统?</strong>',
                         template: '您确定要退出系统吗?',
                         okText: '&nbsp;&nbsp;退出&nbsp;&nbsp;',
                         cancelText: '&nbsp;&nbsp;取消&nbsp;&nbsp;'
                 });

             confirmPopup.then(function (res) {
                     if (res) {
                             ionic.Platform.exitApp();
                         } else {
                             // Don't close
                         }
                 });
         }

     // Is there a page to go back to?
     if ($location.path() == '/tab/items' ) {
             showConfirm();
         } else if ($rootScope.$viewHistory.backView ) {
             console.log('currentView:', $rootScope.$viewHistory.currentView);
             // Go back in history
             $rootScope.$viewHistory.backView.go();
         } else {
             // This is the last page: Show confirmation popup
             showConfirm();
         }

     return false;
    }, 101);

}])

.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
        //设置跨域
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        //设置表单数据提交
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  $stateProvider

    // setup an abstract state for the tabs directive

      .state('login', {
          url: "/login",
          templateUrl: "templates/login.html",
          controller: 'LoginCtrl'
      })
      /*.state('test', {
          url: "/test",
          templateUrl: "templates/test.html",
          controller: 'MainCtrl'
      })*/

    .state('tab', {
      url: "/tab",
      abstract: true,
      templateUrl: "templates/tabs.html"
    })

    // Each tab has its own nav history stack:

    .state('tab.dash', {
      url: '/dash',
      views: {
        'tab-dash': {
          templateUrl: 'templates/tab-dash.html',
          controller: 'DashCtrl'
        }
      }
    })

    .state('tab.items', {
      url: '/items',
      views: {
        'tab-items': {
          templateUrl: 'templates/tab-items.html',
          controller: 'ItemsCtrl'
        }
      }
    })
    .state('tab.item-detail', {
      url: '/device/:itemsId',
      views: {
        'tab-items': {
          templateUrl: 'templates/device-relay.html',
          controller: 'ItemsDetailCtrl'
        }
      }
    })
      .state('tab.item-device', {//设备列表
          url: '/item/:itemsId/:itemName',
          views: {
              'tab-items': {
                  templateUrl: 'templates/item-device.html',
                  controller: 'ItemDeviceCtrl'
              }
          }
      })

      .state('tab.device-relay', {//设备列表
          url: '/relay/:did/:deviceName',
          views: {
              'tab-items': {
                  templateUrl: 'templates/device-relay.html',
                  controller: 'RelayListCtrl'
              }
          }
      })

      .state('tab.scheme', {//定时方案列表
          url: '/scheme/:did/:deviceName/:mode',
          views: {
              'tab-items': {
                  templateUrl: 'templates/scheme.html',
                  controller: 'SchemeCtrl'
              }
          }
      })

      .state('tab.item-device-detail', {//设备详情
          url: '/device/:did/:deviceName',
          views: {
              'tab-items': {
                  templateUrl: 'templates/item-device-detail.html',
                  controller: 'ItemsDeviceDetailCtrl'
              }
          }
      })

    .state('tab.help', {
      url: '/help',
      views: {
        'tab-help': {
          templateUrl: 'templates/tab-help.html'/*,
          controller: 'AccountCtrl'*/
        }
      }
    })
      .state('tab.deviceSwitch', {
          url: '/deviceSwitch',
          views: {
              'tab-help': {
                  templateUrl: 'templates/tab-help-deviceSwitch.html'
              }
          }
      })
      .state('tab.timerSet', {
          url: '/timerSet',
          views: {
              'tab-help': {
                  templateUrl: 'templates/tab-help-timerSet.html'
              }
          }
      })
        //用户密码重置
        .state('tab.reset',{
          url:'/reset',
          views: {
              'tab-dash':{
                  templateUrl: 'templates/reset.html',
                  controller: 'pasResetCtrl'
              }
          }
      })

        //设备详情
      /*.state('tab.device',{
          url:'/device/:itemsId/:deviceID',
          views: {
              'tab-items':{
                  templateUrl: 'templates/item-device-detail.html',
                  controller: 'DeviceCtrl'
              }
          }
      })*/

      .state('logOut',{
        url:'/logOut',
        controller: 'LogOutCtrl'
  })
      //用户密码重置
      .state('tab.sss',{
          url:'/about',
          views: {
              'tab-help': {
                  templateUrl: 'templates/tab-help-about.html'
              }
          }
      })
  // if none of the above states are matched, use this as the fallback
  //$urlRouterProvider.otherwise('/tab/dash');
        $urlRouterProvider.otherwise('/login');
        //$urlRouterProvider.otherwise({
        //  redirectTo: '/login'
    //  });
})

